package com.qersoft.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qersoft.dao.ClassMemberDao;
import com.qersoft.entity.ClassMemberEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@Service
public class ClassMemberServiceImpl implements ClassMemberService {

    private static final Logger logger = LoggerFactory.getLogger(ClassMemberServiceImpl.class);

    @Autowired
    private ClassMemberDao classMemberDao;

    @Override
    @Transactional
    public ClassMemberEntity addClassMember(ClassMemberEntity classMemberEntity) {
        classMemberDao.insert(classMemberEntity);
        return classMemberEntity;  // 返回添加后的班级成员实体
    }

    @Override
    public List<ClassMemberEntity> getClassMembers(Long classId) {
        QueryWrapper<ClassMemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_id", classId);  // 设置查询条件：班级ID等于给定的classId
        return classMemberDao.selectList(queryWrapper);  // 使用 selectList 方法来查询符合条件的班级成员
    }


    @Override
    @Transactional
    public boolean updateClassMember(ClassMemberEntity classMemberEntity) {
        return classMemberDao.updateById(classMemberEntity) > 0;
    }

    @Override
    @Transactional
    public boolean deleteClassMember(Long classMemberId) {
        return classMemberDao.deleteById(classMemberId) > 0;
    }

    @Override
    public ClassMemberEntity getClassMemberById(Long memberId) {
        return classMemberDao.selectById(memberId);
    }
}
